package week4;

import java.io.Serializable;

public class Course implements Serializable,Comparable<Course> {
    private String prefix;
    private int number;
    private String title;
    private String grade;

    // 构造特有信息的course
    public Course (String prefix, int number, String title, String grade)
    {
        this.prefix = prefix;
        this.number = number;
        this.title = title;
        if (grade == null)
        {
            this.grade = "";
        }
        else
        {
            this.grade = grade;
        }
    }

    // 构造特有信息，没有grade的course
    public Course (String prefix, int number, String title)
    {
        this(prefix, number, title, "");
    }

    // 返回prefix
    public String getPrefix()
    {
        return prefix;
    }

    // 返回number
    public int getNumber()
    {
        return number;
    }

    // 返回title
    public String getTitle()
    {
        return title;
    }

    // 返回grade
    public String getGrade()
    {
        return grade;
    }

    // 更改grade
    public void setGrade(String grade)
    {
        this.grade = grade;
    }

    // 如果这门课程已经被上过（如果已经收到成绩），返回true
    public boolean taken()
    {
        return !grade.equals("");
    }

    // 根据这门课程名称（前缀和编号）确定该课程是否等于指定的课程。
    public boolean equals(Object other)
    {
        boolean result = false;
        if (other instanceof Course)
        {
            Course otherCourse = (Course) other;
            if (prefix.equals(otherCourse.getPrefix()) && number == otherCourse.getNumber())
            {
                result = true;
            }
        }
        return result;
    }

    // 创建并返回该课程的字符串表现形式。
    public String toString()
    {
        String result = prefix + " " + number + ": " + title;
        if(!grade.equals(""))
        {
            result += "  [" + grade + "]";
        }
        return result;
    }

    @Override
    public int compareTo(Course course) {
        if (prefix.compareTo(course.prefix) > 0)
        {
            return 1;
        }
        else
        {
            if (prefix.compareTo(course.prefix) < 0)
            {
                return -1;
            }
            else
            {
                if (number > course.number)
                {
                    return 1;
                }
                else
                {
                    if (number < course.number) {
                        return -1;
                    }
                    else {
                        return 0;
                    }
                }
            }
        }
    }
}
